package net.cgsoft.widget;

import android.content.Context;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import net.cgsoft.widget.adapter.CalendarAdapter;
import net.cgsoft.widget.model.DateModel;
import net.cgsoft.widget.model.ScheduleDate;
import net.cgsoft.widget.utils.Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by Youga on 2016/1/6.
 */
public class CalendarFragment extends DialogFragment {

    private static final String TAG = "CalendarFragment";
    Calendar mCalendar = Calendar.getInstance(Locale.CHINA);
    CalendarAdapter mAdapter;
    TextView mCalendarTitle;
    ImageView mPreviousMonth, mNextMonth;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy年M月", Locale.CHINA);
    CalendarFragmentCallBack callBack;

    public static CalendarFragment newInstance(ArrayList<ScheduleDate> dateList) {
        CalendarFragment fragment = new CalendarFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("dateList", dateList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callBack = (CalendarFragmentCallBack) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
        init(rootView);
        return rootView;
    }

    private void init(View rootView) {
        mCalendarTitle = (TextView) rootView.findViewById(R.id.calendarTitle);
        mPreviousMonth = (ImageView) rootView.findViewById(R.id.previousMonth);
        mNextMonth = (ImageView) rootView.findViewById(R.id.nextMonth);
        GridView gridView = (GridView) rootView.findViewById(R.id.gridView);
        ArrayList<ScheduleDate> dateList = (ArrayList<ScheduleDate>) getArguments().getSerializable("dateList");
        mAdapter = new CalendarAdapter(getActivity(), mCalendar, dateList);
        gridView.setAdapter(mAdapter);

        mCalendarTitle.setText(mDateFormat.format(mCalendar.getTime()));

        mPreviousMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendar.add(Calendar.MONTH, -1);
                String month = Util.mMonthFormat.format(mCalendar.getTime());
                if (month.compareTo(mAdapter.getCurrentMonth()) < 0) {
                    mCalendar.add(Calendar.MONTH, 1);
                    Toast.makeText(getActivity(), "不能小于当前月份", Toast.LENGTH_SHORT).show();
                } else {
                    mCalendarTitle.setText(mDateFormat.format(mCalendar.getTime()));
                    callBack.loadCalendarMonth(month);
                }
            }
        });

        mNextMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendar.add(Calendar.MONTH, 1);
                mCalendarTitle.setText(mDateFormat.format(mCalendar.getTime()));
                callBack.loadCalendarMonth(Util.mMonthFormat.format(mCalendar.getTime()));
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DateModel dateModel = (DateModel) mAdapter.getItem(position);
                ScheduleDate scheduleDate = dateModel.getScheduleDate();
                Log.i(TAG, "scheduleDate:" + scheduleDate);
                if (scheduleDate != null && scheduleDate.getNum() > 0) {
                    callBack.calendarItemClick(dateModel.getDate());
                }
            }
        });
    }

    public void refreshDate(ArrayList<ScheduleDate> dateList) {
        mAdapter.refreshDate(mCalendar, dateList);
    }

    public interface CalendarFragmentCallBack {
        void calendarItemClick(String date);

        void loadCalendarMonth(String month);
    }
}
